﻿using System;
using ERP_RORZE_SYSTEM;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_ExchangeRate : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_ExchangeRate()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.addDate.EditValue = DateTime.Now.Date;
            LoadCurrencyList();
        }

        //////////////////////////////////////
        /// Load Currency List
        //////////////////////////////////////
        private void LoadCurrencyList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CurrencyID, CurrencyName, CurrencySymbol FROM [222_Currency] ORDER BY CurrencySymbol";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CurrencySymbol"].ToString() + " | " + dataReader["CurrencyName"].ToString();
                    item.Value = dataReader["CurrencyID"].ToString();
                    this.currencyID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Currency list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string currency_id = null;
            string exchang_rate = null;
            DateTime add_date = new DateTime();
            float rate = 0;

            if (this.addDate.EditValue == null)
            {
                error = "Please select Date";
            }
            else
            {
                add_date = DateTime.Parse(this.addDate.EditValue.ToString());
            }
            if (string.IsNullOrEmpty(error))
            {
                if (this.currencyID.SelectedIndex < 0)
                {
                    error = "Please select Currency";
                }
                else
                {
                    ComboboxItem item = (ComboboxItem)this.currencyID.SelectedItem;
                    currency_id = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.exchangeRate.Text))
                {
                    error = "Please enter Exchange Rate";
                }
                else
                {
                    if (!float.TryParse(this.exchangeRate.Text, out rate))
                    {
                        error = "Invalid Exchange Rate";
                    }
                    else
                    {
                        exchang_rate = this.exchangeRate.Text;
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_ExchangeRate] (ExchangeDate, CurrencyID, ExchangeRate)"
                        + " VALUES (@exchangedate, " + currency_id + ", " + exchang_rate + ")";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@exchangedate", SqlDbType.NVarChar).Value = add_date.Date.ToString("yyyyMMdd");
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    clearForm();
                    MessageBox.Show("Exchange Rate added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Exchange Rate. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
